﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using NUnit.Framework.Constraints;
using Simhopp.Model;
using Simhopp.Database;

namespace NUnit.Framework.Tests
{
    [TestFixture]
    class TestDBConnect
    {
        Judge j1;
        int CompetitionID;
        int JudgeID;
        int DiverID;
        DBConnect dbc;
        Competition comp1;

        [SetUp]
        public void Init()
        {
            dbc = new DBConnect();
        }

        [Test]
        public void AddCompetition()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.International, "Hallsberg", 
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            Console.WriteLine("Comp ID: " + CompetitionID);
            if (CompetitionID != 0)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void UpdateCompetition()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.International, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            dbc.UpdateCompetition(CompetitionID, "DM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            
            Competition comp = dbc.GetCompetitionWithID(CompetitionID);
            if (comp.Name == "DM kval" &&
                comp.Event == CompetitionType.National &&
                comp.Location == "Hallsberg" &&
                comp.Date == new DateTime(2014, 2, 17) &&
                comp.Height == Height.Platform_10m &&
                comp.AgeClass == AgeClass.B &&
                comp.Gender == 'F' &&
                comp.JudgingJudges == 7 &&
                comp.RoundsInCompetition == 5)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void RemoveCompetition()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            JudgeID = dbc.AddJudge("Benny", Countries.Andorra, "hemligt", "benny", CompetitionID);
            dbc.RemoveCompetition(CompetitionID);

            Competition comp = dbc.GetCompetitionWithID(CompetitionID);
            if (comp == null)
                svar = true;
            else
                svar = false;
            
            Assert.AreEqual(svar, true);
        }

        [Test]
        public void GetAllCompetition()
        {
            //Vet inte något bra sätt att testa detta
            bool svar = false;
            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            CompetitionID = dbc.AddCompetition("VM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            List<Competition> CompList = new List<Competition>();
            dbc.GetAllCompetition(CompList);
            if (CompList.Count() >= 2)
                svar = true;
            else
                svar = false;
            Assert.AreEqual(svar, true);
        }

        [Test]
        public void GetCompetitionWithID()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            Console.WriteLine("Comp ID: " + CompetitionID);
            Competition comp = dbc.GetCompetitionWithID(CompetitionID);

            if (comp.Name == "SM kval")
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void AddJudge()
        {
            bool svar = false;
            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            JudgeID = dbc.AddJudge("Benny", Countries.Andorra, "hemligt", "benny", CompetitionID);

            if (JudgeID > 0)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void AddJudgeToCompetition()
        {
            bool svar = false;
            List<Judge> ListOfJudge = new List<Judge>();

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            Console.WriteLine("CompetitionID: " + CompetitionID);
            JudgeID = dbc.AddJudge("Benny", Countries.Andorra, "hemligt", "benny", CompetitionID);
            Console.WriteLine("JudgeID: " + JudgeID);
            CompetitionID = dbc.AddCompetition("VM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            Console.WriteLine("CompetitionID: " + CompetitionID);

            dbc.AddJudgeToCompetition(JudgeID, CompetitionID);
            dbc.GetJudgesWithCompetitionID(CompetitionID, ListOfJudge);

            if (ListOfJudge.Count >= 1)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void UpdateJudge()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            JudgeID = dbc.AddJudge("Benny", Countries.Andorra, "hemligt", "benny", CompetitionID);

            dbc.UpdateJudge(JudgeID, "Björn", Countries.Austria, "hemligt", "benny", CompetitionID);
            Judge j = dbc.GetJudgeWithID(JudgeID);

            if (j.Name == "Björn" &&
                j.Nationality == Countries.Austria &&
                j.Password == "hemligt" &&
                j.Username == "benny")
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void RemoveJudge()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            JudgeID = dbc.AddJudge("Benny", Countries.Andorra, "hemligt", "benny", CompetitionID);

            dbc.RemoveJudge(JudgeID);

            Judge j = dbc.GetJudgeWithID(JudgeID);

            if (j == null)
                svar = false;
            else
                svar = true;

            Assert.AreEqual(svar, false);
        }

        [Test]
        public void GetJudge()
        {
            bool svar = false;
            List<Judge> JudgeList = new List<Judge>();
            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            JudgeID = dbc.AddJudge("Benny", Countries.Andorra, "hemligt", "benny", CompetitionID);
            JudgeID = dbc.AddJudge("Björn", Countries.Azerbaijan, "hemligt", "benny", CompetitionID);
            dbc.GetAllJudge(JudgeList);

            if (JudgeList.Count() >= 2)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(svar, true);
        }

        [Test]
        public void AddDiver()
        {
            bool svar = false;
            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);

            DiverID = dbc.AddDiver("Johan", new DateTime(2000, 05, 05), 'M', 23, 
                55, Countries.Bahrain, CompetitionID);

            if (DiverID > 0)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(true, svar);
        }

        [Test]
        public void AddDiverToCompetition()
        {
            bool svar = false;
            List<Diver> DiverList = new List<Diver>();

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            DiverID = dbc.AddDiver("Johan", new DateTime(2000, 05, 05), 
                'M', 23, 55, Countries.Bahrain, CompetitionID);
            CompetitionID = dbc.AddCompetition("VM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);

            dbc.AddDiverToCompetition(DiverID, CompetitionID, 0, 0);

            dbc.GetDiverWithCompetitionID(CompetitionID, DiverList);

            if (DiverList.Count >= 1)
                svar = true;
            else
                svar = false;

            Assert.AreEqual(true, svar);
        }

        [Test]
        public void UpdateDiver()
        {
            bool svar = false;
            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);

            DiverID = dbc.AddDiver("Johan", new DateTime(2000, 05, 05), 'M', 23, 
                55, Countries.Bahrain, CompetitionID);

            dbc.UpdateDiver(DiverID, "Amanda", new DateTime(1990, 05, 05), 'F', 23,
                55, Countries.Bahrain, CompetitionID);

            Diver d = dbc.GetDiverWithID(DiverID);

            if (d.Name == "Amanda" &&
                d.DateOfBirth == new DateTime(1990, 05, 05) &&
                d.Gender == 'F' )
                svar = true;
            else
                svar = false;
            Assert.AreEqual(true, svar);
        }

        [Test]
        public void RemoveDiver()
        {
            bool svar = false;

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            DiverID = dbc.AddDiver("Johan", new DateTime(2000, 05, 05), 'M', 23,
                55, Countries.Bahrain, CompetitionID);

            dbc.RemoveDiver(DiverID);

            Diver d = dbc.GetDiverWithID(DiverID);

            if (d == null)
                svar = false;
            else
                svar = true;

            Assert.AreEqual(false, svar);
        }

        [Test]
        public void GetDiver()
        {
            bool svar = false;
            List<Diver> DiverList = new List<Diver>();

            CompetitionID = dbc.AddCompetition("SM kval", CompetitionType.National, "Hallsberg",
                new DateTime(2014, 2, 17), Height.Platform_10m, AgeClass.B, 'F', 7, 5);
            DiverID = dbc.AddDiver("Johan", new DateTime(2000, 05, 05), 'M', 23,
                55, Countries.Bahrain, CompetitionID);
            DiverID = dbc.AddDiver("Amanda", new DateTime(1990, 05, 05), 'F', 23,
                55, Countries.Bahrain, CompetitionID);

            dbc.GetAllDiver(DiverList);

            if (DiverList.Count() >= 2)
                svar = true;
            else
                svar = false;
            Assert.AreEqual(true, svar);
        }

        [Test]
        public void AddDive()
        {

        }

        [TearDown]
        public void CleanUp()
        {
            //SqlConnection conn = new SqlConnection();
            //conn.ConnectionString = "Data Source=WIN8DEV;Initial Catalog=SimhoppDeluxeOfDoom;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
            //conn.Open();
            //SqlCommand command = new SqlCommand("DELETE FROM Competition", conn);
            //command.ExecuteNonQuery();

            //conn.Close();

        }
    }
}
